<?php

namespace App\Imports;

use App\Http\Util\Err;
use App\Http\Util\Tool;
use App\Models\AllExam;
use App\Models\QuestionCategory;
use App\Models\Sign;
use App\Models\Cert;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;

class CertImport implements ToCollection
{

    public $date;
    public $student_ids;
    public $checks = '';
    public function __construct($date)
    {
        $this->date = $date;
    }

    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function collection(Collection $rows)
    {
        try{
//            $operateId = AllExam::where('id',$this->all_exam_id)->value('operate_id');
//            $proIds =OperateCate::where('pid',$operateId)->get(['id'])->toArray();
            // TODO: Implement collection() method.
            //解析excel中的数据
//            $proIds = [];
            $data=json_decode(json_encode($rows),true);
            //去掉第一行的标题
            array_splice($data, 0, 1);
            $log = '';
            $list = [];
            //拼接字段入库
            foreach ($data as $row)
            {
                if($row[0]){
//                    $append = DB::table('sign as s')
//                        ->leftjoin('all_exam as e', 's.all_exam_id', '=', 'e.id')
//                        ->where(function ($query) use ($row) {
//                            $query->where('s.exam_student_id',$row[0]);
//                            $query->where('s.is_del', Err::DEL_SHOW);
//                        })
//                        ->select(
//                            's.real_name as real_name',
//                                    's.card_type as card_type',
//                                    's.id_card as id_card',
//                                    'e.cate_id as cate_id',
//                                    'e.level as level',
//                                    's.image as image'
//                        )
//                        ->first();
                    $check = Sign::where('exam_student_id',$row[0])
                        ->where('is_del',Err::DEL_SHOW)
                        ->first();

                    if(!$check){
                        $this->checks .="准考证".$row[0]."<br/>";
                        continue;
                    }
                    $check->certificate = $row[2];
                    $check->occupation = $row[1];
                    $check->cert_date = $this->date ;
                    $check->save();
                    $this->student_ids[] = $row[0];
//                    $append =json_encode($append);
//                    $append = json_decode($append,true);
//                    $append['cate_name'] = QuestionCategory::where('id',$append['cate_id'])->value('name');
//                    switch($append['level']){
//                        case 1:
//                            $append['cate_name'] = '高级技师';
//                            break;
//                        case 2:
//                            $append['cate_name'] = '技师';
//                            break;
//                        case 3:
//                            $append['cate_name'] = '高级';
//                            break;
//                        case 4:
//                            $append['cate_name'] = '中级';
//                            break;
//                        default:
//                            $append['cate_name'] = '初级';
//                            break;
//                    }
                }
            }
        }catch (\Exception $e){
            echo $e->getMessage();
//                    Tool::logError('数据导入失败',$e->getMessage(), $file = 'lumen');
        }
    }


    public function createData($rows)
    {
        //todo
    }
}
